﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.ExportToRTR.CCoExportToRTRCmd
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using DnProcessSimulateCommands.ExportToRTR.Resources;
using DnProcessSimulateCommands.ExportToRTR.Views;
using System;
using System.Windows;
using System.Windows.Threading;
using Tecnomatix.Engineering;
using Tecnomatix.Engineering.Ui;


namespace DnProcessSimulateCommands.ExportToRTR{

internal sealed class CCoExportToRTRCmd : TxButtonCommand
{
  private CCoExportToRTRCmdEnabler m_enabler;
  public static string CmdName = ExportToRTRStringTable.EXPORT_TO_RTR_NAME;

  public CCoExportToRTRCmd() => this.m_enabler = new CCoExportToRTRCmdEnabler();

  public virtual string Category => ExportToRTRStringTable.EXPORT_TO_RTR_CATEGORY;

  public virtual string Name => CCoExportToRTRCmd.CmdName;

  public virtual ITxCommandEnabler CommandEnabler => (ITxCommandEnabler) this.m_enabler;

  public virtual bool Licensed => TxCommandLicenser.RTRIntegrationLicense;

  public virtual string LargeBitmap
  {
    get => "ExportToRTR.Resources.Export_to_Realtime_Robotics_32X32.png";
  }

  public virtual string MediumBitmap
  {
    get => "ExportToRTR.Resources.Export_to_Realtime_Robotics_24X24.png";
  }

  public virtual string Bitmap => "ExportToRTR.Resources.Export_to_Realtime_Robotics_16X16.BMP";

  public virtual ITxImage Image
  {
    get => (ITxImage) TxImageManager.GetImageByKey("ExportForRealtimeToolkit");
  }

  public virtual void Execute(object cmdParams)
  {
    if (cmdParams is CCoExportToRTRCmdParams rtrCommandParams)
    {
      this.ExportToRTRWithParams(rtrCommandParams);
    }
    else
    {
      CUiExportToRTRWindow exportToRtrWindow = new CUiExportToRTRWindow(new CUiExportToRTRWindowViewModel());
      ((Window) exportToRtrWindow).Show();
      exportToRtrWindow.UpdateFromRtr();
    }
  }

  private void ExportToRTRWithParams(CCoExportToRTRCmdParams rtrCommandParams)
  {
    DispatcherFrame exportFrame = new DispatcherFrame();
    bool flag = true;
    Dispatcher currentDispatcher = Dispatcher.CurrentDispatcher;
    CUiExportToRTRWindowViewModel rtrWindowViewModel = new CUiExportToRTRWindowViewModel();
    rtrWindowViewModel.CloseAction = new Action(this.Close);
    rtrWindowViewModel.CurrentDispatcher = currentDispatcher;
    rtrWindowViewModel.ProgressDataHandler = new CUiExportToRTRProgressHandler(200.0, new Action<double>(this.SetProgressValue));
    rtrWindowViewModel.OutputVm.InstallProject = rtrCommandParams.InstallProject;
    rtrWindowViewModel.ContentVm.SegmentTimeCurrentValue = rtrCommandParams.PlanTime;
    rtrCommandParams.StatusProvider.NotifyStartStage("RTR Export Command No UI: Initialize data and operations");
    rtrWindowViewModel.Init(rtrCommandParams.ExportedOperations);
    currentDispatcher.BeginInvoke((Delegate) (() => exportFrame.Continue = false), DispatcherPriority.Background);
    Dispatcher.PushFrame(exportFrame);
    exportFrame.Continue = true;
    rtrCommandParams.StatusProvider.NotifyStartStage("RTR Export Command No UI: Initialize default machine and project name and check for collision pairs");
    rtrWindowViewModel.Initialize();
    currentDispatcher.BeginInvoke((Delegate) (() => exportFrame.Continue = false), DispatcherPriority.Background);
    Dispatcher.PushFrame(exportFrame);
    if (rtrCommandParams.InstallProject && !string.IsNullOrEmpty(rtrCommandParams.MachineName))
    {
      rtrCommandParams.StatusProvider.NotifyStartStage("RTR Export Command No UI: Initializing the requested RTR machine");
      rtrWindowViewModel.OutputVm.MachineName = rtrCommandParams.MachineName;
    }
    rtrCommandParams.StatusProvider.NotifyStartStage("RTR Export Command No UI: Initializing the requested project name to export");
    rtrWindowViewModel.OutputVm.PackageName = rtrCommandParams.ProjectName;
    if (!rtrWindowViewModel.CanExport())
    {
      rtrCommandParams.StatusProvider.NotifyExportFailed("RTR Export Command No UI: Failed initialization", rtrWindowViewModel.ExportMessage);
      flag = false;
    }
    if (!flag)
      return;
    string empty = string.Empty;
    rtrWindowViewModel.OnExport(false, ref empty);
    if (string.IsNullOrEmpty(empty))
      rtrCommandParams.StatusProvider.NotifyExportEnded("RTR Export Command NO UI: Succeeded in export, now installing data", rtrWindowViewModel.ExportMessage, rtrWindowViewModel.OutputVm.MachineName);
    else
      rtrCommandParams.StatusProvider.NotifyExportFailed("RTR Export Command NO UI: Succeeded in export, failed before installing data", empty);
  }

  private void Close()
  {
  }

  private void SetProgressValue(double progressValue)
  {
  }
}
